package com.mashibing.tank;

import com.mashibing.tank.chainofresponsibility.BulletTankCollider;
import com.mashibing.tank.chainofresponsibility.BulletWallCollider;
import com.mashibing.tank.chainofresponsibility.Collider;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;

/**
 * @description: 坦克窗口
 * @author: 余希瑶
 * @date: 2021年06月17日 18:06
 * @version:1.0
 */
public class TankFrame extends Frame implements Serializable{
    public static final TankFrame INSTANCE = new TankFrame();


    static final int GAME_WIDTH = 1080, GAME_HEIGHT = 960;

    public GameModel gm = new GameModel();

    private TankFrame() {
        this.setTitle("tank work");
        //设置x y坐标
        this.setLocation(400, 100);
        //设置宽高
        this.setSize(GAME_WIDTH, GAME_HEIGHT);

        this.addKeyListener(new TankKeyLister());
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }


    Collider collider = new BulletTankCollider();
    Collider collider2 = new BulletWallCollider();

    /**
     * 绘图
     *
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        gm.paint(g);
    }

   transient Image offScreenImage = null;

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    public GameModel getGm() {
        return this.gm;
    }

    private class TankKeyLister extends KeyAdapter {

        /**
         * 键盘操作 按下
         *
         * @param e
         */
        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_S) {
                save();
            } else if (key == KeyEvent.VK_L) {
                load();
            } else {
                gm.getMyTank().keyPressed(e);
                //x++;
            }

        }

        /**
         * 键盘操作 松开
         *
         * @param e
         */
        @Override
        public void keyReleased(KeyEvent e) {
            gm.getMyTank().keyReleased(e);
        }

    }

    public void save() {
        ObjectOutputStream oos = null;
        try {
            File file = new File("C:/yxy/develop/ideaWork/新建文件夹/Tank2021/src/data/tank.dat");
            FileOutputStream fos = new FileOutputStream(file);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(gm);
            oos.flush();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public void load() {
        ObjectInputStream ois = null;
        try {
            File file = new File("C:/yxy/develop/ideaWork/新建文件夹/Tank2021/src/data/tank.dat");
            FileInputStream fis = new FileInputStream(file);
            ois = new ObjectInputStream(fis);
            this.gm = (GameModel) ois.readObject();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static TankFrame getINSTANCE() {
        return INSTANCE;
    }

    public static int getGameWidth() {
        return GAME_WIDTH;
    }

    public static int getGameHeight() {
        return GAME_HEIGHT;
    }

    public Image getOffScreenImage() {
        return offScreenImage;
    }

    public void setOffScreenImage(Image offScreenImage) {
        this.offScreenImage = offScreenImage;
    }
}
